﻿using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotelManager.Service
{
    public class HttpRestClient
    {
        private readonly string apiUrl;
        protected readonly RestClient client;
        public HttpRestClient(string apiUrl)
        {
            this.apiUrl = apiUrl;
            client = new RestClient();
        }
        public async Task<T> ExecuteAsync<T>(BaseRequest baseRequest)
        {
            var request = new RestRequest(apiUrl + baseRequest.Route, baseRequest.Method);
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (baseRequest.Parameter != null)
                request.AddParameter("param", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);
            Uri url = client.BuildUri(request);
            var response = await client.ExecuteAsync(request);

            return JsonConvert.DeserializeObject<T>(response.Content);
        }
        public T ExecuteGetSync<T>(BaseRequest baseRequest)
        {
            var request = new RestRequest(apiUrl + baseRequest.Route, baseRequest.Method);
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (baseRequest.Parameter != null)
                request.AddParameter("param", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);
            Uri url = client.BuildUri(request);
            var response =client.Execute(request);

            return JsonConvert.DeserializeObject<T>(response.Content);
        }
        public T ExecuteSync<T>(BaseRequest baseRequest)
        {
            var request = new RestRequest(apiUrl + baseRequest.Route, baseRequest.Method);
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (baseRequest.Parameter != null)
                request.AddParameter("text/plain", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);
            var response =client.Execute(request);

            return JsonConvert.DeserializeObject<T>(response.Content);
        }
        public async Task<T> ExecutePostAsync<T>(BaseRequest baseRequest)
        {
            var request = new RestRequest(apiUrl + baseRequest.Route, baseRequest.Method);
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (baseRequest.Parameter != null)
                request.AddParameter("text/plain", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);
            var response = await client.ExecuteAsync(request);

            return JsonConvert.DeserializeObject<T>(response.Content);
        }
        public async Task<T> ExecutePostAsync1<T>(BaseRequest baseRequest)
        {
            var request = new RestRequest(apiUrl + baseRequest.Route, baseRequest.Method);
            request.AddHeader("Content-Type", baseRequest.ContentType);
            if (baseRequest.Parameter != null)
                request.AddParameter("text/plain", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);
            var response = await client.ExecuteAsync(request);

            return JsonConvert.DeserializeObject<T>(response.Content);
        }
        public T ExecuteUpload<T>(BaseRequest baseRequest,string filePath)
        {
            var request = new RestRequest(apiUrl + baseRequest.Route, baseRequest.Method);
            baseRequest.ContentType = "multipart/form-data";
            request.AddHeader("Content-Type", baseRequest.ContentType);
            request.AddFile("file", filePath);//file一定要和后端参数绑定一致
            if (baseRequest.Parameter != null)
                request.AddParameter("text/plain", JsonConvert.SerializeObject(baseRequest.Parameter), ParameterType.RequestBody);
            var response = client.ExecutePost(request);

            return JsonConvert.DeserializeObject<T>(response.Content);
        }
    }
}
